package com.example.Uchsz.service.impl;

import com.example.Uchsz.entity.User;
import com.example.Uchsz.mapper.UserMapper;
import com.example.Uchsz.service.RegisterService;
import com.example.Uchsz.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static cn.hutool.crypto.SecureUtil.md5;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    UserMapper userMapper;

    public int register(User user) {
        MD5Utils md5 = new MD5Utils();
        user.setPassword(md5.digest(user.getPassword()));//对密码字段存库，加密

        return userMapper.register(user);
    }

    @Override
    public int yanzhengma(User yanzhengma) {
        return 0;
    }




    //根据用户名从DB查询用户信息
    public User searchUser(int phone) {
        return userMapper.searchUser(phone);
    }

    @Override
    public String login(int phone, String password) {
        String md5str=MD5Utils.digest(password);
//        String md5str=md5(password);
        User user=userMapper.searchUser(phone);
        if(user == null){
            return "用户未注册";
        }
        user.getPassword();
        String passwordOld=user.getPassword();
        if(passwordOld!=null&&passwordOld.equals(md5str)){
            return "密码验证通过";
        }
        return "密码验证失败";
    }


}
